using DataDynamics.ActiveReports;
using SS;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace SM.core
{
	internal class Doctor
	{
		private Func objFunc = new Func();
		public Doctor()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(string ID, string Name, string Address, string Phone, string Company, string Acc, string Photo)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabDoctor", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Name", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Address", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Phone", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Company", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Acc", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Photo", SqlDbType.VarChar);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["Name"].Value = Name;
				sqlCommand.Parameters["Address"].Value = Address;
				sqlCommand.Parameters["Phone"].Value = Phone;
				sqlCommand.Parameters["Company"].Value = Company;
				sqlCommand.Parameters["Acc"].Value = Acc;
				sqlCommand.Parameters["Photo"].Value = Photo;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		private byte[] ImageToStream(string fileName)
		{
			Bitmap bitmap = new Bitmap(fileName);
			MemoryStream memoryStream = new MemoryStream();
			bitmap.Save(memoryStream, ImageFormat.Bmp);
			return memoryStream.ToArray();
		}
		public void getImageFromDataBase(Picture picImage, string userName)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT TOP 1 Photo FROM [TabDoctor] WHERE [Acc] = '" + userName + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			try
			{
				string filename = (string)sqlCommand.ExecuteScalar();
				picImage.Image = Image.FromFile(filename);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadGridView(DataGridView dgv, BindingNavigator bdn)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT ID, Name, Address, Phone, Company, Acc, Photo FROM [TabDoctor] ORDER BY [Name]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet);
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables[0];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getListDoctor(DataGridView dgv, string Name)
		{
			try
			{
				dgv.AutoGenerateColumns = false;
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string selectCommandText = "SELECT ID, Name, Address, Phone, Company, Acc, Photo FROM [TabDoctor] WHERE (Upper(NAME) LIKE N'%" + Name.ToUpper() + "%')  ORDER BY [Name]";
				SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
				DataSet dataSet = new DataSet();
				sqlDataAdapter.Fill(dataSet);
				dgv.DataSource = dataSet.Tables[0];
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public string getDoctorName(string Acc, ref string doctorName)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT ID, Name FROM [TabDoctor] WHERE [Acc] = '" + Acc + "'";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = sqlDataReader["ID"].ToString();
				doctorName = sqlDataReader["Name"].ToString();
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string savePrescription(string ID, DateTime DateCreate, string DoctorID, string PatientName, string PatientAge, string PatientSex, string PatientAddress, string PatientPhone, string DoctorName, string PatientID, string _Desease, string UserGuide, string DoctorGuide)
		{
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabPrescription", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("TID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["TID"].Direction = ParameterDirection.Output;
				sqlCommand.Parameters.Add("DateCreate", SqlDbType.DateTime);
				sqlCommand.Parameters.Add("DoctorID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("PatientName", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("PatientAge", SqlDbType.Int);
				sqlCommand.Parameters.Add("PatientSex", SqlDbType.SmallInt);
				sqlCommand.Parameters.Add("PatientAddress", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("PatientPhone", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("DoctorName", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("PatientID", SqlDbType.Int);
				sqlCommand.Parameters.Add("Desease", SqlDbType.NVarChar, 1500);
				sqlCommand.Parameters.Add("UserGuide", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("DoctorGuide", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["TID"].Value = ID;
				sqlCommand.Parameters["DateCreate"].Value = DateCreate;
				sqlCommand.Parameters["DoctorID"].Value = DoctorID;
				sqlCommand.Parameters["PatientName"].Value = PatientName;
				sqlCommand.Parameters["PatientAge"].Value = PatientAge;
				sqlCommand.Parameters["PatientSex"].Value = PatientSex;
				sqlCommand.Parameters["PatientAddress"].Value = PatientAddress;
				sqlCommand.Parameters["PatientPhone"].Value = PatientPhone;
				sqlCommand.Parameters["DoctorName"].Value = DoctorName;
				sqlCommand.Parameters["PatientID"].Value = PatientID;
				sqlCommand.Parameters["Desease"].Value = _Desease;
				sqlCommand.Parameters["UserGuide"].Value = UserGuide;
				sqlCommand.Parameters["DoctorGuide"].Value = DoctorGuide;
				sqlCommand.ExecuteNonQuery();
				string text = sqlCommand.Parameters["TID"].Value.ToString();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = text;
			}
			catch
			{
				result = "";
			}
			return result;
		}
		public string saveTmpPrescription(string ID, DateTime DateCreate, string deseaseID, string Note, string DoctorID)
		{
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabTmpPrescription", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("TID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["TID"].Direction = ParameterDirection.Output;
				sqlCommand.Parameters.Add("DateCreate", SqlDbType.DateTime);
				sqlCommand.Parameters.Add("DeseaseID", SqlDbType.Int);
				sqlCommand.Parameters.Add("Note", SqlDbType.NVarChar);
				sqlCommand.Parameters.Add("DoctorID", SqlDbType.NVarChar);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["TID"].Value = ID;
				sqlCommand.Parameters["DateCreate"].Value = DateCreate;
				sqlCommand.Parameters["DeseaseID"].Value = deseaseID;
				sqlCommand.Parameters["Note"].Value = Note;
				sqlCommand.Parameters["DoctorID"].Value = DoctorID;
				sqlCommand.ExecuteNonQuery();
				string text = sqlCommand.Parameters["TID"].Value.ToString();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = text;
			}
			catch
			{
				result = "";
			}
			return result;
		}
		public int saveTmpPrescriptionDetail(string billID, DataTable objTable)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabTmpPrescriptionDetail", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("billID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("ProductID", SqlDbType.Int);
				sqlCommand.Parameters.Add("ProductName", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("ProductUnit", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("ProductNumber", SqlDbType.Float);
				sqlCommand.Parameters.Add("ProductGuide", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters["billID"].Value = billID;
				for (int i = 0; i < objTable.Rows.Count; i++)
				{
					sqlCommand.Parameters["ProductID"].Value = objTable.Rows[i]["ProductID"].ToString();
					sqlCommand.Parameters["ProductName"].Value = objTable.Rows[i]["ProductName"].ToString();
					sqlCommand.Parameters["ProductUnit"].Value = objTable.Rows[i]["ProductUnit"].ToString();
					sqlCommand.Parameters["ProductNumber"].Value = objTable.Rows[i]["ProductNumber"].ToString();
					sqlCommand.Parameters["ProductGuide"].Value = objTable.Rows[i]["ProductGuide"].ToString();
					sqlCommand.ExecuteNonQuery();
				}
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = 1;
			}
			return result;
		}
		public int savePrescriptionDetail(string billID, DataTable objTable)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabPrescriptionDetail", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("billID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("ProductID", SqlDbType.Int);
				sqlCommand.Parameters.Add("ProductName", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("ProductUnit", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("ProductNumber", SqlDbType.Float);
				sqlCommand.Parameters.Add("ProductGuide", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("ProductPrice", SqlDbType.Float);
				sqlCommand.Parameters.Add("ProductMoney", SqlDbType.Float);
				sqlCommand.Parameters["billID"].Value = billID;
				for (int i = 0; i < objTable.Rows.Count; i++)
				{
					sqlCommand.Parameters["ProductID"].Value = objTable.Rows[i]["ProductID"].ToString();
					sqlCommand.Parameters["ProductName"].Value = objTable.Rows[i]["ProductName"].ToString();
					sqlCommand.Parameters["ProductUnit"].Value = objTable.Rows[i]["ProductUnit"].ToString();
					sqlCommand.Parameters["ProductNumber"].Value = objTable.Rows[i]["ProductNumber"].ToString();
					sqlCommand.Parameters["ProductGuide"].Value = objTable.Rows[i]["ProductGuide"].ToString();
					sqlCommand.Parameters["ProductPrice"].Value = objTable.Rows[i]["ProductPrice"].ToString();
					sqlCommand.Parameters["ProductMoney"].Value = objTable.Rows[i]["ProductMoney"].ToString();
					sqlCommand.ExecuteNonQuery();
				}
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = 1;
			}
			return result;
		}
		public DataTable getPrescriptionDetail(string billID)
		{
			DataTable result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT ProductID,ProductName,ProductUnit,ProductNumber,ProductGuide, ProductPrice, ProductMoney FROM [TabPrescriptionDetail] WHERE [billID] = '" + billID + "'", sqlConnection);
				DataSet dataSet = new DataSet();
				sqlDataAdapter.Fill(dataSet, "TabPrescriptionDetail");
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = dataSet.Tables["TabPrescriptionDetail"];
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = null;
			}
			return result;
		}
		public DataTable getPrescriptionDetailSale(string billID)
		{
			DataTable result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT ProductID,ProductName,ProductUnit,ProductNumber,ProductNumberSale,ProductGuide, ProductPrice, ProductMoney FROM [TabPrescriptionDetail] WHERE [billID] = '" + billID + "' AND ProductNumberSale > 0", sqlConnection);
				DataSet dataSet = new DataSet();
				sqlDataAdapter.Fill(dataSet, "TabPrescriptionDetail");
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = dataSet.Tables["TabPrescriptionDetail"];
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = null;
			}
			return result;
		}
		public DataTable getTmpPrescriptionDetail(string billID)
		{
			DataTable result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT ProductID,ProductName,ProductUnit,ProductNumber, ProductGuide FROM [TabTmpPrescriptionDetail] WHERE [billID] = '" + billID + "'", sqlConnection);
				DataSet dataSet = new DataSet();
				sqlDataAdapter.Fill(dataSet, "TabTmpPrescriptionDetail");
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = dataSet.Tables["TabTmpPrescriptionDetail"];
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = null;
			}
			return result;
		}
		public int delPrescription(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_DelPrescription", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public int delTmpPrescription(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_DelTmpPrescription", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public int delItem(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "DELETE [TabDoctor] WHERE [ID] = '" + ID + "'";
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public string CheckEmpExitstInOutBill(string ID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			try
			{
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT [ID] FROM [TabBill] WHERE [_DoctorID] = '" + ID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					result = "1";
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
			}
			catch
			{
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string cusPrescription(string ID, string _Name, string _Age, string _Sex, string _Address, string _Phone)
		{
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabPatients", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Direction = ParameterDirection.Output;
				sqlCommand.Parameters.Add("_Name", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("_Age", SqlDbType.Int);
				sqlCommand.Parameters.Add("_Sex", SqlDbType.Int);
				sqlCommand.Parameters.Add("_Address", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("_Phone", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["_Name"].Value = _Name;
				sqlCommand.Parameters["_Age"].Value = _Age;
				sqlCommand.Parameters["_Sex"].Value = _Sex;
				sqlCommand.Parameters["_Address"].Value = _Address;
				sqlCommand.Parameters["_Phone"].Value = _Phone;
				sqlCommand.ExecuteNonQuery();
				string text = sqlCommand.Parameters["ID"].Value.ToString();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = text;
			}
			catch
			{
				result = "";
			}
			return result;
		}
	}
}
